<?php
require 'common.inc.php';
if (!function_exists('getallheaders')) {
    function getallheaders() {
        $headers = array();
        foreach ($_SERVER as $name => $value) {
            if (substr($name, 0, 5) == 'HTTP_') {
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        return $headers;
    }
}
$headers = getallheaders();

if($headers['Token'] != token()){
	$arr = array(
		'status' => 0,
		'msg' => '非法操作'
	);
	returnJson($arr);
}
if(empty($action)){
	returnJson(array(
		'status' => 0,
		'msg' => '参数错误'
	));
}

switch ($action) {
	case 'get_server':
		$data['os'] = PHP_OS;
		$data['ip'] = $_SERVER['SERVER_ADDR'] . ':' . $_SERVER['SERVER_PORT'];
		$data['php_sapi_name'] = php_sapi_name();
		$data['php_version'] = PHP_VERSION;
		$data['mysql_version'] = mysql_get_server_info();
		$data['upload_max_filesize'] = ini_get('upload_max_filesize');
		$data['max_execution_time'] = ini_get('max_execution_time') . "秒";
		$data['memory_limit'] = ini_get('memory_limit');
		$data['disk_free_space'] = round((@disk_free_space(".") / (1024 * 1024)), 2) . 'M';
		$data['server_time'] = date("Y年n月j日 H:i:s");
		returnJson(array(
			'status' => 1,
			'msg' => '执行成功',
			'data' => $data 
		));
	break;

	case 'put_pic':
		$itemid = isset($_GET['itemid']) ? $_GET['itemid'] : 0;
		if(empty($itemid)){
			returnJson(array(
				'status' => 0,
				'msg' => '参数错误'
			));
		}
		$res = DB::get_one("SELECT content FROM {$DT_PRE}article_data_23 WHERE itemid = $itemid");
		if(!empty($res['content'])){
			$pattern = "/<img.*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.jpeg]))[\'|\"].*?[\/]?>/";
    		preg_match_all($pattern,$res['content'],$match); 
    		if(!empty($match[1])){
    			$domain = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
    			$thumb = '';
    			foreach ($match[1] as $k => $v) {
	    			if(strpos($v, 'http') !== false && strpos($v, $_SERVER['HTTP_HOST']) === false){
	    				$pic = $domain.grabImage($v);
	    				if(empty($thumb)){
	    					$thumb = $pic;
	    				}
	    				$res['content'] = str_replace($v, $pic, $res['content']);
	    			}
	    		}
	    		DB::query("UPDATE ".DT_PRE."article_data_23 SET content='".$res['content']."' WHERE itemid = ".$itemid);
	    		DB::query("UPDATE ".DT_PRE."article_23 SET thumb='".$thumb."' WHERE itemid = ".$itemid);
    		}
		}
		
		returnJson(array(
			'status' => 1,
			'msg' => '执行成功'
		));
	break;
}


function token(){
	$date = date('Y.m.d.H',time());
	$token = md5(md5($date.'xiexueyuan.com'));
	return $token;
}

function returnJson($arr){
	header('Content-Type:application/json; charset=utf-8');
	exit(json_encode($arr,JSON_UNESCAPED_UNICODE));
}

function grabImage($url, $filename = "") {
	if ($url == "") return false;
	if ($filename == "") {
		$ext = strrchr($url, ".");
		if ($ext != ".png" && $ext != ".gif" && $ext != ".jpg" && $ext != ".jpeg") return false;
		$filename = mt_rand(1000,9999) . substr(time(),-5) . $ext;
	} 
	$filepath = '/file/upload/'.date('Ym').'/'.date('d').'/';
	if(!is_dir(DT_ROOT.$filepath)){
		mkdir(DT_ROOT.$filepath,0777,true);
	}
	ob_start();//打开输出
	readfile($url);//输出图片文件
	$img = ob_get_contents();//得到浏览器输出
	ob_end_clean();//清除输出并关闭
	$size = strlen($img);//得到图片大小
	$fp = @fopen(DT_ROOT.$filepath.$filename, "a");
	fwrite($fp, $img);//向当前目录写入图片文件，并重新命名
	fclose($fp);
	return $filepath.$filename;//返回新的文件名
} 

